package com.nue.service.impl;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nue.mapper.RegisterMapper;
import com.nue.pojo.Register;
import com.nue.service.IDoctorService;
import com.nue.service.IRegisterService;
import com.nue.util.CommonResult;
import com.nue.util.HisConstants;
import org.springframework.stereotype.Component;
import com.auth0.jwt.JWT;

import javax.annotation.Resource;
import java.time.LocalDate;

@Component
public class DoctorServiceImpl implements IDoctorService {
    @Resource
    IRegisterService registerService;
    @Override
    public CommonResult list(Integer type,String token) {
        DecodedJWT decode = JWT.decode(token);
        Integer id =decode.getClaim("id").asInt();
        Integer deptId=decode.getClaim("deptId").asInt();
        QueryWrapper<Register> qu=new QueryWrapper<>();
        if(type!=null){
            if(type==0){
                qu.eq("doctor_id",id);
            }else {
                qu.eq("dept_id",deptId);
            }
            qu.in("status",HisConstants.REGISTER_REGIST,HisConstants.REGISTER_DEAL);
            qu.eq("visittime", LocalDate.now());
            return CommonResult.success(registerService.list(qu));
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult submit(Register register) {
        register.setStatus(HisConstants.REGISTER_DEAL);
        boolean i=registerService.updateById(register);
        if(i){
            return CommonResult.success("添加病历信息成功");
        }
        return CommonResult.failed();
    }
}
